package czh.cocoa.ui.impl;

import czh.cocoa.graphics.CGRect;
import czh.cocoa.graphics.CGSize;
import czh.cocoa.ui.UIEvent;
import czh.cocoa.ui.UIImage;
import czh.cocoa.ui.UITouch;
import czh.cocoa.ui.UIView;

public class UISlider extends UIControl {

	private final static float THUMB_INSET = 5;
	
	private float mValue;
	
	private float mMinimumValue;
	
	private float mMaximumValue;
	
	private UIView mThumbView;
	
	private UIImageView mProgressView;
	
	private float mLastTouchX;
	
	private boolean mContinous;
	
	private boolean mValueDidChanged;
	
	public UISlider() {
		
	}
	
	public UISlider(CGRect frame) {
		super(frame);
	}
	
	protected void init() {
		super.init();
		
		mValue = 0;
		mMinimumValue = 0;
		mMaximumValue = 1;
		
		UIImage track_img = UIImage.imageNamed(NSTR("cocoa/slider_track"));
		UIImageView track_view = new UIImageView(track_img);
		track_view.setAutoresizingMask(UIViewAutoresizingFlexibleWidth);
		track_view.setContentStretch(CGRectMake(0.4f, 0, 0.1f, 1)); 
		
		UIImage progress_img = UIImage.imageNamed(NSTR("cocoa/slider_progress"));
		UIImageView progress_view = new UIImageView(progress_img);
		progress_view.setContentStretch(CGRectMake(0.9f, 0, 0.1f, 1)); 
		
		UIImage thumb_img = UIImage.imageNamed(NSTR("cocoa/slider_thumb"));
		UIImageView thumb_view = new UIImageView(thumb_img);
		
		CGRect frame = thumb_view.bounds();
		frame.size.width += THUMB_INSET * 2;
		setFrame(frame);
		
		CGRect track_frame = track_view.frame();
		track_frame.size.width = frame.size.width - THUMB_INSET * 2;
		track_frame.origin.x = THUMB_INSET;
		track_frame.origin.y = (frame.size.height - track_frame.size.height) / 2;
		track_view.setFrame(track_frame);
		
		CGRect progress_frame = progress_view.frame();
		progress_frame.size.width = 0;
		progress_frame.origin.x = THUMB_INSET;
		progress_frame.origin.y = (frame.size.height - progress_frame.size.height) / 2;
		progress_view.setFrame(progress_frame);
		
		addSubview(track_view);
		addSubview(progress_view);
		addSubview(thumb_view);
		
		mThumbView = thumb_view;
		mProgressView = progress_view;
		mContinous = true; 
	}
	
	public void setValue(float value) {
		if (value > mMaximumValue) {
			value = mMaximumValue;
		} else if (value < mMinimumValue) {
			value = mMinimumValue;
		}
		if (mValue != value) {
			mValue = value;
			float progress = (value - mMinimumValue) / (mMaximumValue - mMinimumValue);
			
			float width = (bounds().size.width - mThumbView.bounds().size.width) * progress;
			
			CGRect thumb_frame = mThumbView.frame();
			thumb_frame.origin.x = width;
			mThumbView.setFrame(thumb_frame);
			
			CGRect progress_frame = mProgressView.frame();
			progress_frame.size.width = Math.max(width + THUMB_INSET, 0); 
			
			mProgressView.setFrame(progress_frame);
		}
	}
	
	public boolean beginTrackingWithTouchWithEvent(UITouch touch, UIEvent event) {
		mLastTouchX = touch.locationInView(this).x;
		return super.beginTrackingWithTouchWithEvent(touch, event);
	}
	
	public boolean continueTrackingWithTouchWithEvent(UITouch touch, UIEvent event) {
		float x = touch.locationInView(this).x;
		CGRect thumb_frame = mThumbView.frame();
		float progress = (thumb_frame.origin.x + x - mLastTouchX) / 
							(bounds().size.width - thumb_frame.size.width);
		float value = progress * (mMaximumValue - mMinimumValue) + mMinimumValue;
		
		if (mValue != value) {
			setValue(value);
			if (mContinous) {
				sendActionsForControlEvents(UIControlEventValueChanged);
			} else {
				mValueDidChanged = true;
			}
		}
		
		mLastTouchX = x;
		
		return super.continueTrackingWithTouchWithEvent(touch, event);
	}
	
	public void endTrackingWithTouchWithEvent(UITouch touch, UIEvent event) {
		if (!mContinous && mValueDidChanged) {
			sendActionsForControlEvents(UIControlEventValueChanged);
		}
		mValueDidChanged = false;
		super.endTrackingWithTouchWithEvent(touch, event);
	}
	
}
